<?php 
try {
	include_once('db/constants.php');
	include_once('db/authors_ranking_db.php');
	include_once('db/general_functions_db.php');
	include_once('db/error_log_db.php');
	include_once('facebook_login.php');

	$current_file = "uploadContent";
	include_once("users_flow_logger.php");

	define("AUTHORS_SHOWN_PER_PAGE", "7");
	define("AMOUNT_BEST_AUTHORS_TO_SHOW", 25);
	define("MAX_PAGES_OPTIONS", "5");
	
	
	$authors_json = get_best_author_db(AMOUNT_BEST_AUTHORS_TO_SHOW, $user_profile['id']);
	$authors = json_decode( $authors_json );
	
	$number_pages = ceil( count($authors) / AUTHORS_SHOWN_PER_PAGE );
	
	$category_id = $_REQUEST['category_id'];
	
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" style="overflow-x: hidden">
<head>
	<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
	<title><?php echo HTML_TITLE;?></title>
	<link rel="stylesheet" href="css/style.css" type="text/css" media="all" />
	<!--[if lte IE 6]><link rel="stylesheet" href="css/ie6.css" type="text/css" media="all" /><![endif]-->
	
	<script src="js/jquery-1.4.1.min.js" type="text/javascript"></script>
	<script src="js/spin.js" type="text/javascript"></script>
	<script src="js/wrap_content.js?<?php echo REVISION;?>" type="text/javascript"></script>
	
	<script>
		var AUTHORS_SHOWN_PER_PAGE = <?php echo AUTHORS_SHOWN_PER_PAGE; ?>;
		var authors = JSON.parse( '<?php echo $authors_json; ?>' );
		var currentPage = 1;
		var numberPages = <?php echo $number_pages; ?>;
		
		function nextPage() {
			if (Math.ceil(authors.length/AUTHORS_SHOWN_PER_PAGE) > currentPage ) {
				currentPage++;
				
				showPageAuthor();
				showPageButtons();
			} 			
		}
		
		function prevPage() {
			if (currentPage > 1) {
				currentPage--;
				
				showPageAuthor();
				showPageButtons();
			}
		}
		
		function goToPage(number) {
			if (Math.ceil(authors.length/AUTHORS_SHOWN_PER_PAGE) >= number ) {
				currentPage = number;
				
				showPageAuthor();
				showPageButtons();
			} 
		}
		
		function showPageAuthor() {
			var to = (currentPage*AUTHORS_SHOWN_PER_PAGE < authors.length) ? currentPage*AUTHORS_SHOWN_PER_PAGE : authors.length;
			
			for (var i = (currentPage - 1)*AUTHORS_SHOWN_PER_PAGE; i < to; i++) {
				showAuthorAt(i - (currentPage - 1)*AUTHORS_SHOWN_PER_PAGE, authors[i]);
			}
			
			for (var i = to; i < currentPage*AUTHORS_SHOWN_PER_PAGE; i++) {
				var index = i - (currentPage - 1)*AUTHORS_SHOWN_PER_PAGE;
				
				document.getElementById('author-div-' + index).style.display = 'none';
				document.getElementById('hr-' + index).style.display = 'none';
			}
		}
		
		function showAuthorAt(index, author) {
			document.getElementById('author-div-' + index).style.display = '';
			document.getElementById('hr-' + index).style.display = '';
			
			document.getElementById('ranking-td-' + index).innerHTML = author.rank + '.';
			document.getElementById('author-img-' + index).src = 'https://graph.facebook.com/' + author.user_id + '/picture';
			document.getElementById('author-td-' + index).innerHTML = author.first_name + ' ' + author.last_name;
			document.getElementById('points-td-' + index).innerHTML = author.points + ' <?php echo POINTS; ?>';
			document.getElementById('content-list-a-' + index).href = 'publishedContent.php?user_id=' + author.user_id + '&first_name=' + author.first_name + '&last_name=' + author.last_name;
		}
		
		function showPageButtons() {
			var MAX_PAGES_OPTIONS = <?php echo MAX_PAGES_OPTIONS; ?>;
			var pagesInnerHTML = '';
			
			if (MAX_PAGES_OPTIONS >= numberPages) {
				for (var i = 1; i <= numberPages; i++) {
					if (i == currentPage)
						pagesInnerHTML += '<a id="page-link-' + i + '" class="pages-link-number-selected" href="#" onclick="goToPage(' + i+ ')" >';
					else
						pagesInnerHTML += '<a id="page-link-' + i + '" class="pages-link-number" href="#" onclick="goToPage(' + i + ')" >';
						
					pagesInnerHTML += i;
						
					pagesInnerHTML += '</a>';
				}
			} else {
				var pagesEachSide = Math.floor(MAX_PAGES_OPTIONS/2);
				
				if (currentPage + pagesEachSide <= numberPages) {
					var startingPage = (pagesEachSide >= currentPage) ? 1 : currentPage - pagesEachSide;
					var endPage =  startingPage + MAX_PAGES_OPTIONS;
				} else {
					var startingPage = numberPages - MAX_PAGES_OPTIONS + 1;
					var endPage =  numberPages + 1;
				}
				
				for (var i = startingPage; i < endPage; i++) {
					if (i == currentPage)
						pagesInnerHTML += '<a id="page-link-' + i + '" class="pages-link-number-selected" href="#" onclick="goToPage(' + i + ')" >';
					else
						pagesInnerHTML += '<a id="page-link-' + i + '" class="pages-link-number" href="#" onclick="goToPage(' + i + ')" >';
						
					pagesInnerHTML += i;
						
					pagesInnerHTML += '</a>';
				}
			}

			if (document.getElementById('pages') != null)
				document.getElementById('pages').innerHTML = pagesInnerHTML;
		}
		
	</script>
</head>
<body>

	<div id="fb-root"></div>
	<script>(function(d, s, id) {
	  var js, fjs = d.getElementsByTagName(s)[0];
	  if (d.getElementById(id)) return;
	  js = d.createElement(s); js.id = id;
	  js.src = "//connect.facebook.net/es_LA/all.js";
	  fjs.parentNode.insertBefore(js, fjs);
	}(document, 'script', 'facebook-jssdk'));</script>


    <script>		
	window.fbAsyncInit = function() {
	FB.init({
	  appId : '<?php echo APP_ID;?>',
	  xfbml : true,
	  version : 'v2.0'
	});
	};
	(function(d, s, id){
         var js, fjs = d.getElementsByTagName(s)[0];
         if (d.getElementById(id)) {return;}
         js = d.createElement(s); js.id = id;
         js.src = "//connect.facebook.net/es_LA/sdk.js";
         fjs.parentNode.insertBefore(js, fjs);
       }(document, 'script', 'facebook-jssdk'));
      
	function postFeed(description) {
		FB.ui({
		method: 'feed',
		link: '<?php echo APP_CANVAS; ?>',
		picture: 'http://fbrell.com/f8.jpg',
		name: 'Name',
		caption: 'Caption',
		description: description
		}, function(response){
			alert(print_r(response));
			});
	}
      
    </script>

<?php 
$headerFirstOption = SEE_MY_PUBLICATIONS;
$headerSecondOption = CATEGORIES;
$headerThirdOption = PUBLISH;

$headerFirstUrl = PUBLISH_CONTENT."?coming_from=".$current_file;
$headerSecondUrl = CATEGORIES_URL."?coming_from=".$current_file;
$headerThirdUrl = UPLOAD_CONTENT."?coming_from=".$current_file;

$headerFirstClass = "button";
$headerSecondClass = "button-highlighted-green";
$headerThirdClass = "button-highlighted-red";

include_once("header_redirects.php"); 
include_once("header.php"); 
?>

<!-- Main -->
<div id="main">


	<div class="published-content-main-div">
		
		<!-- Cols -->
		<div style="height: 660px;">
			<div class="cl">&nbsp;</div>
			
			<!-- Col -->
			<div class="col" align="center" style="float: none; ">
				<h3><?php echo BEST_AUTHORS;?></h3>

				<hr style="align:center; margin: 0 auto; width: 350px;"/>
				
				<?php
				for ($i = 0; $i < AUTHORS_SHOWN_PER_PAGE; $i++) {
					
					echo '<div id="author-div-'.$i.'">';
						echo '<table border="0px" class="best-author-page-table" cellpadding="0px" cellspacing="0px">';
							echo '<tr>';
								echo '<td id="ranking-td-'.$i.'" valign="top" class="ranking-number" ></td>';
								echo '<td rowspan="2" valign="middle" >';
									echo '<img id="author-img-'.$i.'" />';
								echo '</td>';
								echo '<td id="author-td-'.$i.'" rowspan="2" valign="middle" class="author-name-list"></td>';
							echo '</tr>';
							echo '<tr>';
								echo '<td></td>';
							echo '</tr>';
							echo '<tr>';
								echo '<td></td>';
								echo '<td id="points-td-'.$i.'" style="font-size: 10px; padding-top: 3px;"></td>';
								echo '<td align="right" height="5px">';
									echo '<a id="content-list-a-'.$i.'" class="more">'.SEE_ALL_HIS_CONTENT.'</a>';
								echo '</td>';
							echo '</tr>';
						echo '</table>';
					echo '</div>';
				
					echo '<hr style="align:center; margin: 0 auto; width: 350px;" id="hr-'.$i.'"/>';
				}
				?>
				
				<?php
				if ($number_pages > 1) {
					echo '<table border="0px" class="page-navigation-buttons">';
					echo '<tr>';
						echo '<td> <a class="previous-page-button" onclick="prevPage()" href="#"></a> </td>';
						echo '<td id="pages" class="pages"> ';
						echo '</td>';
						echo '<td> <a class="next-page-button" onclick="nextPage()" href="#"></a> </td>';
					echo '</tr>';
					echo '</table>';
				} ?>
				
			</div>
			<!-- End Col -->
					
		</div>
	</div>
</div>
<!-- End Main -->

<script>
	
	showPageAuthor();

	var spinnerTarget = document.getElementById('main');
	var spinner = new Spinner().spin(spinnerTarget);
	spinner.stop();
	
	showPageButtons();
</script>

</body>
</html>
